<?php
App::uses('AppController', 'Controller');
/**
 * Usuarios Controller
 *
 * @property Usuario $Usuario
 */
class UsuariosController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		//$this->Usuario->recursive = 0;
		//$this->set('usuarios', $this->paginate());
		
		$this->paginate = array('limit' => 10);
		$usuarios = $this->paginate('Usuario');
		$this->set(compact('usuarios'));
	}
	
	public function getStatusservidors() {
		$statusservidors = $this->Usuario->Statusservidor->find('list', array('fields' => array('id', 'statusservidor')));
		
		$this->set(compact('statusservidors'));
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Usuario->id = $id;
		if (!$this->Usuario->exists()) {
			throw new NotFoundException(__('Invalid usuario'));
		}
		$this->set('usuario', $this->Usuario->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	
	public function beforeFilter() {
		parent::beforeFilter();
		//$this->Auth->allow('add');
	}
	
	public function login() {
		$this->redirect($this->Auth->redirect());
		
		/*if($this->Auth->login()) {
			$this->redirect($this->Auth->redirect());
		} else {
			$this->Session->setFlash(__('Usuário ou senha inválidos!'));
		}*/
	}
	
	public function logout() {
		$this->redirect($this->Auth->logout());
	}
	
	
	public function beforeSave($data) {
		$data['Usuario']['passwd'] = md5($data['Usuario']['passwd']);

		return $data;
	}
	
	
	
	public function add() {
		if ($this->request->is('post')) {
			$this->Usuario->create();
			
			$this->request->data = self::beforeSave($this->request->data);
			
			if ($this->Usuario->save($this->request->data)) {
				$this->Session->setFlash(__('The usuario has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The usuario could not be saved. Please, try again.'));
			}
		}
		$statusservidors = $this->Usuario->Statusservidor->find('list');
		$this->set(compact('statusservidors'));
		
		//self::getStatusservidors();
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Usuario->id = $id;
		if (!$this->Usuario->exists()) {
			throw new NotFoundException(__('Invalid usuario'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			
			$this->request->data = self::beforeSave($this->request->data);
			
			if ($this->Usuario->save($this->request->data)) {
				$this->Session->setFlash(__('O usuário foi editado com sucesso!'));
				$this->redirect(array('action' => 'index')); //o sistema eh redirecionado para o index
			} else {
				$this->Session->setFlash(__('The usuario could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Usuario->read(null, $id); //atualiza o formulario de listagem
		}
		$statusservidors = $this->Usuario->Statusservidor->find('list');
		$this->set(compact('statusservidors'));
		
		//self::getStatus();
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Usuario->id = $id;
		if (!$this->Usuario->exists()) {
			throw new NotFoundException(__('Invalid usuario'));
		}
		if ($this->Usuario->delete()) {
			$this->Session->setFlash(__('Usuario deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Usuario was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
